package net.sxnic.workflow.utils;

import java.util.List;

import net.sxnic.ugr.dept.Dept;
import net.sxnic.ugr.dept.DeptManager;
import net.sxnic.ugr.user.User;
import net.sxnic.ugr.user.UserManager;
import net.sxnic.ugr.utils.UgrManagerUtils;
import net.sxnic.workflow.WorkflowConstants;
import net.sxnic.workflow.task.util.TaskUtils;
import net.sxnic.workflow.wfentity.WfEntity;

import org.apache.commons.lang.StringUtils;

public class HuiqianUtils {
	/**
	 * 处理会签 处理后格式 厅内会签:pzy,lm
	 */
	public static String doHuiqian(String taskName, String nextAssignee,
			WfEntity wfentity) {
		// TODO 厅内会签-->无会签的问题
		if (taskName.equals(WorkflowConstants.SEND_NEW_NIGAO)) {

			String temp = TaskUtils.findSepcailAssignee(nextAssignee, "内部会签");
			if (wfentity.getStr2().equals("002")) {
				return temp;
			} else if (wfentity.getStr2().equals("004")) {
				return StringUtils.replace(temp, "内部会签", "内外会签");
			} else {
				return "";
			}
		} else {
			return wfentity.getStr5();
		}
	}

	/**
	 * 处理会签 处理后格式 厅内会签:pzy,lm
	 */
	public static String formatHuiqian(String nextAssignee,
			String huiqianCate) {
		
		if(StringUtils.isBlank(nextAssignee)){
			return "";
		}
		// TODO 厅内会签-->无会签的问题
		String temp = TaskUtils.findSepcailAssignee(nextAssignee, "内部会签");

		if (huiqianCate.equals("002")) {
			return temp;
		} else if (huiqianCate.equals("004")) {
			return StringUtils.replace(temp, "内部会签", "内部会签");
		} else {
			return "";
		}
	}

	/**
	 * 回显会签的列表
	 * 
	 * @param huiqian
	 *            厅内会签：user1,user2,user3
	 * @return 厅内会签：user1(RealName),user2(RealName),user3(RealName)
	 */
	public static String showHuiqian(String huiqian) {
		if (StringUtils.isBlank(huiqian)) {
			return "";
		}

		String[] huiqians = StringUtils.split(
				StringUtils.substringAfter(huiqian, WorkflowConstants.SPLIT_COLON),
				WorkflowConstants.SPLIT_COMMA);

		String temp = "";
		UserManager userManager = UgrManagerUtils.getUserManager();
		for (String hq : huiqians) {
			temp = temp + userManager.getDeptByUser(hq).getName()
					+ WorkflowConstants.SPLIT_COMMA;
		}

		return StringUtils.removeEnd(temp, WorkflowConstants.SPLIT_COMMA);
	}

	/**
	 * 回显会签的html
	 * 
	 * @param huiqian
	 *            厅内会签:chuzhang1,chuzhang2,chuzhang3
	 * @return <input.... />
	 */
	public static String showHuiqianHtml(String huiqian) {
		if (StringUtils.isBlank(huiqian)) {
			return "";
		}

		UserManager userManager = UgrManagerUtils.getUserManager();
		DeptManager deptManager = UgrManagerUtils.getDeptManager();

		// String huiqianType = StringUtils.substringBefore(huiqian,
		// WorkflowConstants.SPLIT_COLON);
		String huiqians = StringUtils.substringAfter(huiqian,
				WorkflowConstants.SPLIT_COLON);

		StringBuffer sb = new StringBuffer();

		// 所有职能处室，除厅领导之外
		List<Dept> list = deptManager.findPureFunctionDepart();
		sb.append("<ul class='row'>");
		for (Dept g : list) {

			User uuu = userManager.findGroupLeaderByDept(g.getCode());
			if (uuu == null) {
				continue;
			}

			sb.append("<li>");
			sb.append("<input type='checkbox' name='nextAssignee' value='"
					+ "厅内会签"
					+ TaskUtils.RECEIVES_SEPARATOR
					+ uuu.getUsername() // 处长username
					+ "' "
					+ ChuanyueUtils.isSelectedChuanyue(huiqians,
							uuu.getUsername()) + "  />" + g.getName() // 处室名
					+ " ");
			sb.append("</li>");
		}
		sb.append("</ul>");

		return sb.toString();
	}

}
